| Conditions | 1 |
| Paths | 1 |
| Total Lines | 147 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | jQuery( function( $ ) { |
||
| 2 | |||
| 3 | /** |
||
| 4 | * JS meta_box field actions. |
||
| 5 | */ |
||
| 6 | var cmb2MetaBoxField = { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Initialize meta box fields behavior. |
||
| 10 | */ |
||
| 11 | init: function() { |
||
| 12 | this.showHideOnLoad(); |
||
| 13 | this.fieldTypeChange(); |
||
| 14 | this.fieldNameChange(); |
||
| 15 | this.displayUsageFunctions(); |
||
| 16 | this.bindActionsToCmb2Events(); |
||
| 17 | }, |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Show / hide field rows based on field_type_select selected value. |
||
| 21 | * |
||
| 22 | * showHideMain |
||
| 23 | * |
||
| 24 | * |
||
| 25 | * |
||
| 26 | */ |
||
| 27 | showHideMain: function( fieldTypeSelect ) { |
||
| 28 | var fieldTypeVal = $( fieldTypeSelect ).val(); |
||
| 29 | var fieldSet = $( fieldTypeSelect ).closest( '.cmb-field-list' ); |
||
| 30 | $( '.cmb-row.cmb_hide_field', fieldSet ).hide(); |
||
| 31 | $( '.cmb-row.' + fieldTypeVal, fieldSet ).show(); |
||
| 32 | }, |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Show / hide field rows based on field_type_select selected value. |
||
| 36 | * |
||
| 37 | * showHideMain |
||
| 38 | * |
||
| 39 | * |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | showHideSide: function() { |
||
| 43 | var isRepeatable = $( '#_cmb2_repeatable_group' ).is( ':checked' ); |
||
| 44 | var repeatableOptions = $( '.cmb2-id--cmb2-group-description, .cmb2-id--cmb2-entry-name' ); |
||
| 45 | repeatableOptions.hide(); |
||
| 46 | if ( isRepeatable ) { |
||
| 47 | repeatableOptions.show(); |
||
| 48 | } |
||
| 49 | }, |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Use the showHideMain() function on each field group's field rows on page load. |
||
| 53 | * |
||
| 54 | * showHideMainOnLoad |
||
| 55 | * |
||
| 56 | * |
||
| 57 | * |
||
| 58 | */ |
||
| 59 | showHideOnLoad: function() { |
||
| 60 | var fieldTypeSelects = $( '.field_type_select' ); |
||
| 61 | fieldTypeSelects.each( function() { |
||
| 62 | cmb2MetaBoxField.showHideMain( this ); |
||
| 63 | } ); |
||
| 64 | cmb2MetaBoxField.showHideSide(); |
||
| 65 | |||
| 66 | }, |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Use the showHideMain() function on this field group's field rows on field_type_select change. |
||
| 70 | * |
||
| 71 | * fieldTypeChange |
||
| 72 | * |
||
| 73 | * |
||
| 74 | * |
||
| 75 | */ |
||
| 76 | fieldTypeChange: function() { |
||
| 77 | $( '.cmb2-metabox' ).on( 'change', '.field_type_select', function( e ) { |
||
| 78 | var fieldTypeSelect = e.target; |
||
| 79 | cmb2MetaBoxField.showHideMain( fieldTypeSelect ); |
||
| 80 | } ); |
||
| 81 | }, |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Use the showHideMain() function on this field group's field rows on field_type_select change. |
||
| 85 | * |
||
| 86 | * fieldTypeChange |
||
| 87 | * |
||
| 88 | * |
||
| 89 | * |
||
| 90 | */ |
||
| 91 | isRepeatableChanged: function() { |
||
| 92 | $( '#_cmb2_repeatable_group' ).on( 'change', function( e ) { |
||
|
|
|||
| 93 | cmb2MetaBoxField.showHideSide(); |
||
| 94 | } ); |
||
| 95 | }, |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Update the usage code snippets when field_name changes. |
||
| 99 | * |
||
| 100 | * fieldNameChange |
||
| 101 | * |
||
| 102 | * |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | fieldNameChange: function() { |
||
| 106 | $( '.cmb2-metabox' ).on( 'change keyup', '.field_name', function() { |
||
| 107 | this.displayUsageFunctions(); |
||
| 108 | }.bind( this ) ); |
||
| 109 | }, |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Convert field name to usage code snippet. |
||
| 113 | * |
||
| 114 | * displayUsageFunctions |
||
| 115 | * |
||
| 116 | * |
||
| 117 | * |
||
| 118 | */ |
||
| 119 | displayUsageFunctions: function() { |
||
| 120 | var fieldName = $( '.field_name' ); |
||
| 121 | var wrapper = fieldName.closest( '.cmb-field-list' ); |
||
| 122 | var getPostMeta = $( '.get_post_meta', wrapper ); |
||
| 123 | var cmbf = $( '.cmbf', wrapper ); |
||
| 124 | var fieldNameVal = fieldName.val().toLowerCase().replace( / /g, '_' ); |
||
| 125 | getPostMeta.val( 'get_post_meta( get_the_ID(), \'_' + fieldNameVal + '\', true );' ); |
||
| 126 | cmbf.val( 'cmbf( get_the_ID(), \'_' + fieldNameVal + '\' );' ); |
||
| 127 | }, |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Run the show / hide functions after reaptabel groups have been manipulated. |
||
| 131 | * |
||
| 132 | * bindActionsToCmb2Events |
||
| 133 | * |
||
| 134 | * |
||
| 135 | * |
||
| 136 | */ |
||
| 137 | bindActionsToCmb2Events: function() { |
||
| 138 | |||
| 139 | $( document ).on( 'click', '.cmb-add-group-row, .cmb-remove-group-row, .cmb-shift-rows', function() { |
||
| 140 | cmb2MetaBoxField.showHideOnLoad(); |
||
| 141 | } ); |
||
| 142 | } |
||
| 143 | }; |
||
| 144 | |||
| 145 | cmb2MetaBoxField.init(); |
||
| 146 | |||
| 147 | } ); |
||
| 148 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.